// 页面加载完成后执行
document.addEventListener('DOMContentLoaded', function() {
    // 1. 猜数字游戏逻辑
    const guessForm = document.getElementById('guessForm');
    const guessInput = document.getElementById('guessInput');
    const messageDiv = document.getElementById('message');
    const historyList = document.getElementById('historyList');
    const resetBtn = document.getElementById('resetBtn');

    // 游戏状态
    let secretNumber = generateSecretNumber();
    let guessCount = 0;
    let isGameOver = false;

    // 生成1-100的随机数
    function generateSecretNumber() {
        return Math.floor(Math.random() * 100) + 1;
    }

    // 重置游戏
    function resetGame() {
        secretNumber = generateSecretNumber();
        guessCount = 0;
        isGameOver = false;
        guessInput.value = '';
        messageDiv.innerHTML = '';
        historyList.innerHTML = '';
        messageDiv.classList.remove('success', 'warning', 'error');
        guessInput.disabled = false;
        resetBtn.style.display = 'none';
        showMessage('请猜一个1-100之间的数字', 'warning');
    }

    // 显示消息
    function showMessage(message, type) {
        messageDiv.textContent = message;
        messageDiv.classList.remove('success', 'warning', 'error');
        messageDiv.classList.add(type);
    }

    // 添加猜谜历史
    function addToHistory(guess, result) {
        const li = document.createElement('li');
        li.innerHTML = `<strong>第${++guessCount}次猜测 ${guess}:</strong> ${result}`;
        historyList.appendChild(li);
    }

    // 验证猜测
    function validateGuess() {
        if (isGameOver) return;

        const guess = parseInt(guessInput.value);
        if (isNaN(guess) || guess < 1 || guess > 100) {
            showMessage('请输入1-100之间的数字', 'error');
            return;
        }

        guessInput.value = '';
        guessInput.focus();

        if (guess === secretNumber) {
            // 猜对了
            addToHistory(guess, '恭喜你，猜对了！');
            showMessage(`恭喜！你用了${guessCount}次猜对了数字${secretNumber}`, 'success');
            isGameOver = true;
            guessInput.disabled = true;
            resetBtn.style.display = 'inline-block';
        } else if (guess < secretNumber) {
            // 猜小了
            addToHistory(guess, '猜小了，再大一点');
            showMessage('猜小了，再大一点', 'warning');
        } else {
            // 猜大了
            addToHistory(guess, '猜大了，再小一点');
            showMessage('猜大了，再小一点', 'warning');
        }
    }

    // 绑定事件
    if (guessForm) {
        guessForm.addEventListener('submit', function(e) {
            e.preventDefault();
            validateGuess();
        });
    }

    if (resetBtn) {
        resetBtn.addEventListener('click', resetGame);
    }

    // 2. 登录表单验证
    const loginForm = document.getElementById('loginForm');
    const usernameInput = document.getElementById('username');
    const passwordInput = document.getElementById('password');

    if (loginForm) {
        loginForm.addEventListener('submit', function(e) {
            e.preventDefault();
            const username = usernameInput.value.trim();
            const password = passwordInput.value;

            if (!username || !password) {
                alert('用户名和密码不能为空');
                return;
            }

            // 这里可以添加实际的登录验证逻辑
            // 示例：简单验证后跳转
            alert('登录成功！');
            window.location.href = '/game';
        });
    }

    // 3. 注册表单验证
    const registerForm = document.getElementById('registerForm');
    const regUsernameInput = document.getElementById('regUsername');
    const regPasswordInput = document.getElementById('regPassword');
    const regConfirmPasswordInput = document.getElementById('regConfirmPassword');

    if (registerForm) {
        registerForm.addEventListener('submit', function(e) {
            e.preventDefault();
            const username = regUsernameInput.value.trim();
            const password = regPasswordInput.value;
            const confirmPassword = regConfirmPasswordInput.value;

            if (!username || !password || !confirmPassword) {
                alert('所有字段都不能为空');
                return;
            }

            if (password !== confirmPassword) {
                alert('两次输入的密码不一致');
                return;
            }

            // 这里可以添加实际的注册逻辑
            alert('注册成功！请登录');
            window.location.href = '/login';
        });
    }
});